package com.auth.project.cn.util;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

public final class ThreadPoolUtil {
    private static int capacity = 5;
    //不公平
    private static ArrayBlockingQueue FAIR_DEFAULT = new ArrayBlockingQueue<>(capacity, true);
    private static ArrayBlockingQueue UNFAIR_DEFAULT = new ArrayBlockingQueue<>(capacity, false);

    private final static ThreadPoolExecutor THREAD_POOL_EXECUTOR = createDefaultFairABQThreadPoolExecutor(6, 10, 5, TimeUnit.SECONDS);
    public static ThreadPoolExecutor createFixedThreadPool() {
        return THREAD_POOL_EXECUTOR;
    }


    public static ThreadPoolExecutor createThreadPoolExecutor(int corePoolSize,
                                                              int maximumPoolSize,
                                                              long keepAliveTime,
                                                              TimeUnit unit,
                                                              BlockingQueue<Runnable> workQueue) {
        return new ThreadPoolMDCExecutor(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue);
    }

    public static ThreadPoolExecutor createDefaultFairABQThreadPoolExecutor(int corePoolSize,
                                                                            int maximumPoolSize,
                                                                            long keepAliveTime,
                                                                            TimeUnit unit) {
        return createThreadPoolExecutor(corePoolSize, maximumPoolSize, keepAliveTime, unit, UNFAIR_DEFAULT);
    }

    public static ThreadPoolExecutor createDefaultUnFairABQThreadPoolExecutor(int corePoolSize,
                                                                              int maximumPoolSize,
                                                                              long keepAliveTime,
                                                                              TimeUnit unit) {
        return createThreadPoolExecutor(corePoolSize, maximumPoolSize, keepAliveTime, unit, UNFAIR_DEFAULT);
    }

    public static ThreadPoolExecutor createDefaultABQThreadPoolExecutor(int corePoolSize,
                                                                        int maximumPoolSize,
                                                                        long keepAliveTime,
                                                                        TimeUnit unit, int capacity, boolean fair) {
        return createThreadPoolExecutor(corePoolSize, maximumPoolSize, keepAliveTime, unit, new ArrayBlockingQueue<>(capacity, fair));
    }
}
